<?php


namespace App\Common;


use App\Conf;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;
use App\TenxunCloud;
use Vod\VodUploadClient;
use Vod\Model\VodUploadRequest;

class UploadFile
{
    /**
     * 上传七牛云
     * @param $filePath
     * @param $name
     * @return array|string
     * @throws \Exception
     */
    public function uploadFile($filePath, $name)
    {
        if (!isset(pathinfo($name)['extension'])) {
            $pathInfo = pathinfo($filePath);
            $name .= "." . $pathInfo['extension'] ?? "txt";
        }
        $uploadMgr = new UploadManager();
        $auth = new Auth(Conf::get('qiniu.accessKey'), Conf::get('qiniu.secretKey'));
        $token = $auth->uploadToken(Conf::get('qiniu.bucket'));
        list($ret, $error) = $uploadMgr->putFile($token, $name, $filePath);
        if ($error !== null) {
            return ["err" => 1, "msg" => $error, "data" => ""];
        }
        return Conf::get('qiniu.domin') . $ret["key"];
    }

    /**
     * 删除七牛云
     * @param $name
     * @return bool
     */
    public function delFile($name)
    {
        $auth = new Auth(Conf::get('qiniu.accessKey'), Conf::get('qiniu.secretKey'));
        $config = new \Qiniu\Config();
        $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);
        list($ret, $error) = $bucketManager->delete(Conf::get('qiniu.bucket'), $name);
        if ($error != null) {
            return $error;
        }
        return true;
    }


    /**
     * 上传腾讯视频
     * @param $filePath
     * @return array|string
     */
    public function uploadVedio($filePath)
    {
        $client = new VodUploadClient(Conf::get('tenxxun.accessKey'), Conf::get('tenxxun.secretKey'));
        $req = new VodUploadRequest();
        $req->MediaFilePath = $filePath;
        $req->ClassId = 745084;
        $req->MediaName = pathinfo($filePath)['filename'];
        try {
            $rsp = $client->upload("ap-shanghai", $req);
            return [$rsp->FileId, $rsp->MediaUrl];
        } catch (\Exception $e) {
            // 处理上传异常
            return $e->getMessage();
        }
    }


    public function test($id)
    {
        $client = TenxunCloud::getClient();
        $req = new \TencentCloud\Vod\V20180717\Models\DescribeMediaInfosRequest();
        $req->FileIds = [$id];
        $rsp = $client->DescribeMediaInfos($req);
        var_dump($rsp->serialize());


    }

    public function Task($id)
    {


        $client = TenxunCloud::getClient();

        $req = new \TencentCloud\Vod\V20180717\Models\ProcessMediaRequest();
        $req->FileId = $id;
        $rsp = $client->ProcessMedia($req);
        if (isset($rsp->serialize()['TaskId'])) {
            $taskId = $rsp->serialize()['TaskId'];
            $req = new \TencentCloud\Vod\V20180717\Models\DescribeTaskDetailRequest();
            $req->TaskId = $taskId;
            $rsp = $client->DescribeTaskDetail($req);
            var_dump($rsp->serialize());
            die();
        }

    }


    public function getClassId()
    {
        $client = TenxunCloud::getClient();
        $req = new \TencentCloud\Vod\V20180717\Models\DescribeAllClassRequest();
        $rsp = $client->DescribeAllClass($req);
        var_dump($rsp->serialize());die();
    }


}